package com.atguigu.servlet.model;

import com.atguigu.bean.Book;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.servlet.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

public class BookServlet extends BaseServlet {
    //book?flag=bookManager
    protected void bookManager(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //查询books表中的所有的数据
        BookService bookService = new BookServiceImpl();
        List<Book> allBook = bookService.findAllBook();
        request.setAttribute("allBook",allBook);
        this.processTemplate("manager/book_manager",request,response);
    }
    //book?flag=orderManager
    protected void orderManager(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processTemplate("manager/order_manager",request,response);
    }
    //book?flag=toBookEdit
    protected void toBookEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processTemplate("manager/book_edit",request,response);
    }
    //book?flag=addbook
    protected void addbook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取添加书的数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        //创建一个book对象
        Book book = new Book();
        //将数据放入book对象中
        try {
            BeanUtils.populate(book,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用方法
        BookService bookService = new BookServiceImpl();
        boolean b = bookService.saveBook(book);
        if(b){
            response.sendRedirect(request.getContextPath()+"/book?flag=bookManager");
        }else{
            response.sendRedirect(request.getContextPath()+"/book?flag=toBookEdit");
        }
    }
   //book?flag=deleteBook
   protected void deleteBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //获取id值，根据id值进行删除
       String id = request.getParameter("id");
       System.out.println("id = " + id);
       //调用方法
       BookService bookService = new BookServiceImpl();
       bookService.deleteBookById(id);
       //删除完之后再回到图书管理页面（通过重定向进行跳转）book?flag=bookManager
       response.sendRedirect(request.getContextPath()+"/book?flag=bookManager");
   }
   //updateBook
   protected void toUpdateBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
       /**
        * 1.先获取id
        * 2.根据id值进行查询信息
        * 3.将查询到的数据进行数据共享（进行数据回传）
        */
       //1.先获取id
       String id = request.getParameter("id");
       //2.根据id值进行查询信息(调用方法)
       BookService bookService = new BookServiceImpl();
       Book book = bookService.selectBookById(id);
       //3.将查询到的数据进行数据共享（进行数据回传）
       request.setAttribute("book",book);
       this.processTemplate("manager/book_update",request,response);
   }
   //updateBook
   protected void updateBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //获取修改参数
       Map<String, String[]> parameterMap = request.getParameterMap();
       //创建book对象
       Book book = new Book();
       //将参数放入book对象中
       try {
           BeanUtils.populate(book,parameterMap);
       } catch (IllegalAccessException e) {
       } catch (InvocationTargetException e) {
           e.printStackTrace();
       }
       //调用修改方法
       BookService bookService = new BookServiceImpl();
       bookService.updateBookById(book);
       //跳转到图书管理页面（通过重定向进行跳转）book?flag=bookManager
       response.sendRedirect(request.getContextPath()+"/book?flag=bookManager");
   }
  //book?flag=tocart
  protected void tocart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        this.processTemplate("cart/cart",request,response);

  }
}

